<?php
/**
 * Redundant class. Not yet used 
 */ 
class Clu_Controller_Action_Mini
{

    public $view;   
    public $db;
    public $holderWeights;
    public $regionId = 4;
    public $boxWeight = 1;
    public $miniRequest;
    
    public function __construct($miniRequest) //just a simple array
    {
        $this->miniRequest = $miniRequest;
        $this->init();  
        
    }
    
    public function init()
    {
        $this->db = Zend_Db_Table::getDefaultAdapter(); //TODO: Remove this when the Models are fine
        //$this->holderWeights = Zend_Registry::get('holderWeights');
        if (isset($this->miniRequest['regionId']))
            $this->regionId = $this->miniRequest['regionId'];
            
        if (isset($this->miniRequest['boxWeight']))
            $this->boxWeight = $this->miniRequest['boxWeight'];
            
    	set_include_path(  
            get_include_path() . PATH_SEPARATOR .  
            APP_PATH.'/modules/'.$this->miniRequest['module'] 
        );
            
        $this->initView();
    }
    
    public function initView()
    {
    	//TODO: use the other view or new Zend_View here???
        //$this->view = $this->miniRequest['view'];
        
        $this->view = new Zend_View();
        //$this->view = Zend_Registry::get('cluView');
        //die_a($this->view);
        $this->view->addBasePath(APP_PATH.'/modules/'.$this->miniRequest['module'] .'/views' );
        
        $this->view->setEncoding('UTF-8');
        $this->view->doctype('XHTML1_STRICT');
        $this->view->addHelperPath(
            APP_PATH . '/library/Clu/View/Helper',
            'Clu_View_Helper'
        );
		
        
        //add include path
    }
    

    public function renderToHolder ($script, $useSessLang = false, $theme = null, $regionId = null, $weight = null)
    {
        $this->holderWeights = Zend_Registry::get('holderWeights');
        $sess = new Zend_Session_Namespace(CLU_NS);
        if (!$theme)
        	$theme = "commonTheme";
         //current viewed theme
        else
        {
        	if ($theme === true)
				$theme = isset($sess->userTheme) ? $sess->userTheme:Zend_Registry::get('configuration')->general->defaultTheme;
			/*        		
			else //specific theme
	    		$theme = $theme;
	    	*/
        }
		        
        if ($useSessLang)
            $script = $theme.'/'.$sess->viewLang . '/' . $script;
        else        
            $script = $theme."/nolang/" . $script; /*todo: turn to 'nolang'*/
//        die($script);
        if (!$regionId)
            $regionId = $this->regionId; //default
        if (!$weight)
            $weight = $this->boxWeight;
        
        $i = $weight;   

        $name = 'region' . $regionId . '_box';
        
        while (array_key_exists($name.$i, $this->holderWeights))
        {
            $i++;
        }
        $placeholder = $name.$i;
        //Zend_Registry::get('logger')->log("miniBlock return:" . $placeholder, Zend_Log::INFO);
        $this->view->placeholder($placeholder)->captureStart();
        echo $this->view->render($script);
        $this->view->placeholder($placeholder)->captureEnd();
        $this->holderWeights[$placeholder] = $weight; 
        Zend_Registry::set('holderWeights', $this->holderWeights);
    }
    
    public function setHolder ($content, $regionId = null, $weight = null )
    {
        $this->holderWeights = Zend_Registry::get('holderWeights');
    
        //$script = Zend_Registry::get("viewLang") . '/' . $script;
        $i = $weight;
        $name = 'region' . $regionId . '_box';
        while (array_key_exists($name.$i, $this->holderWeights))
        {
            $i++;
        }
        $placeholder = $name.$i;

        $this->view->placeholder($placeholder)->captureStart();
        echo $content;
        $this->view->placeholder($placeholder)->captureEnd();
        $this->holderWeights[$placeholder] = $weight; 
        Zend_Registry::set('holderWeights', $this->holderWeights);
        
    }
    
    protected function addMiniAction($miniRequest)
    {
    	$module = $miniRequest['module'];
    	$controller = Ucwords($miniRequest['controller']); //Uppercase word
    	$action = $miniRequest['action'];
    	//$controller = $miniRequest['controller']; /* blockController in most cases*/
    	$sess = new Zend_Session_Namespace();
    	$lang = $sess->viewLang;
    	// get the mini controller
    	$controllerClassName = Ucwords($module) . "_" . $controller . "Controller";//Home_GomeController
    	/*
    	set_include_path(  
            get_include_path() . PATH_SEPARATOR .  
            APP_PATH.'/modules/'.$module . '/controllers' 
        );
        */
    	require_once (APP_PATH.'/modules/'.$module . '/controllers/' . $controller . 'Controller.php');

    	//add the include path
		$miniRequest['view'] = $this->view;
    	$minController = new $controllerClassName($miniRequest);
    	$a = $action . "Action";
    	$minController->{$a}();
    }    
    
        /* error handler for action that doesn't exist
	*/
    public function __call($method, $args)
    {
    	Zend_Registry::get('logger')->log("Action: " . $method, Zend_Log::INFO);
    	die($method . ' does not exists in class ' . get_class($this));
        return;
        // TODO: in deployment mode, forward to an error page
        if ('Action' == substr($method, -6)) {
            // If the action method was not found, render the error
            // template
            //return $this->render('error');
            die ("action <b>" . $method . "</b> for module <b>". $this->getRequest()->getModuleName() ."</b> does not exist <br/>");
        }

        // all other methods throw an exception
        throw new Exception('Invalid method "'
                            . $method
                            . '" called',
                            500);
    }
    
}
?>